﻿namespace Indigo.RenderingTemplateDemo
{
    using System.Linq;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.Workflow;
    using Microsoft.SharePoint.WebPartPages;
    using System;

    /// <summary>
    /// This form component checks for the status of the workflows running on an item
    /// </summary>
    public class CheckWorkflowStatus : FormComponent
    {
        private bool? isWorkflowActive = null;

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void OnLoad(System.EventArgs e)
        {
            if (this.IsWorkflowActive)
            {
                // disable the form items
                var parentControl = this.GetParentWebPart();
                if (parentControl != null)
                {
                    foreach (var controls in parentControl.FindControls<FormComponent>())
                    {
                        if (controls is CheckWorkflowStatus)
                        {
                            continue;
                        }

                        controls.ControlMode = SPControlMode.Display;
                    }
                }
            }
        }

        /// <summary>
        /// Gets the parent template control.
        /// </summary>
        /// <returns></returns>
        private ListFormWebPart GetParentWebPart()
        {
            var parent = this.Parent;
            while (parent != null)
            {
                if (parent is ListFormWebPart)
                {
                    break;
                }

                parent = parent.Parent;
            }

            return parent as ListFormWebPart;
        }

        /// <summary>
        /// Gets the name of the default rendering template.
        /// </summary>
        /// <returns>A <see cref="T:System.String"/> object that names a rendering template. </returns>
        protected override string DefaultTemplateName
        {
            get
            {
                return "CheckWorkflowStatus";
            }
        }

        /// <summary>
        /// Gets or sets a value that indicates whether a server control is rendered as UI on the page.
        /// </summary>
        /// <returns>true if the control is visible on the page; otherwise false.
        ///   </returns>
        public override bool Visible
        {
            get
            {
                return this.IsWorkflowActive && this.ControlMode == SPControlMode.Edit;
            }
            set
            {
                base.Visible = value;
            }
        }

        private bool IsWorkflowActive
        {
            get
            {
                if (!this.isWorkflowActive.HasValue)
                {
                    this.isWorkflowActive = false;

                    if (this.ListItem != null)
                    {
                        if (this.ListItem.Workflows
                            .Cast<SPWorkflow>().Any(x => x.InternalState == SPWorkflowState.Running))
                        {
                            this.isWorkflowActive = true;
                        }
                    }
                }

                return this.isWorkflowActive.Value;
            }

        }
    }
}
